package eu.news.servlet.admin.system;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import eu.jk.base.security.AuthFilter;
import eu.news.data.hibernate.dao.HB_OperatorDAO;
import eu.news.data.pojo.Operator;
import eu.news.kits.JsonUtil;

/**
 * Servlet implementation class ListUser
 */
public class ListUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/admin/system/user/list.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final int BLOCK_SIZE=50;
		int page=0;
		String _page=request.getParameter("page");
		if(AuthFilter.isDigit(_page))
			page=Integer.parseInt(_page);
		HB_OperatorDAO opDao=new HB_OperatorDAO();
		StringBuffer buffer=new StringBuffer("{\"items\":[");
		for(Operator op:opDao.getAll(page*BLOCK_SIZE, BLOCK_SIZE)){
			JsonUtil json=new JsonUtil();
			json.put("name", op.getName());
			json.put("type", op.getType());
			buffer.append(json);
			buffer.append(",");
		}
		if(buffer.length()>10)
			buffer.deleteCharAt(buffer.length()-1);
		buffer.append("]}");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(buffer);
	}

}
